<?php

namespace app\admin\controller\sys;

use app\admin\controller\Base;
use app\admin\controller\Qiniu;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Minapp extends Base
{

    public function index()
    {
        //dump(config());
        return view();
    }

    public function add()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = new \app\admin\validate\Minapp();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                DB::name('minapp')->insert($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            return view();
        }
    }

    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            Db::startTrans();
            try {
                Db::name('minapp')->where(['id' => $post['id']])->delete();
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        }
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $data['count'] = Db::name('minapp')
            ->where($map)
            ->count();
        $data['data'] = Db::name('minapp')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
foreach ($data['data'] as &$item){
    switch ($item['code_type']){
        case 1:
            $item['code_type']='统一社会信用代码';
            break;
        case 2:
            $item['code_type']='组织机构代码';
            break;
        case 3:
            $item['code_type']='营业执照注册号';
            break;
        default:
            $item['code_type']='--';

    }
}

        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


}